<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        let arr = [1, 8, 5, 9, 3, 0, 6];
        for (let i = 0, length = arr.length; i < length; i++) {
            for (let j = 0; j < length - i - 1; j++) {  //减一是为了防止溢出。当length为7，i为0时，j有可能值为6。下面有j + 1，arr[7]是undefined的
                if (arr[j] > arr[j + 1])    //当前面的值大于后面的值的时候，就交换位置
                    [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
            }
        }
        console.log(arr);
    </script>
</body>

</html>